package com.apobates.jforum2.troll.nest.service;

import com.apobates.jforum2.troll.nest.FrontFeignClientConfig;
import com.apobates.jforum2.troll.notices.stub.vo.LetterDigestVo;
import com.apobates.jforum2.troll.notices.stub.vo.LetterInboxNoticeVo;
import com.apobates.jforum2.troll.notices.stub.vo.LetterInboxVo;
import com.apobates.jforum2.troll.notices.stub.vo.LetterOutboxVo;
import com.apobates.jforum2.troll.utils.core.EntityPageResult;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 版块组(卷)服务客户端
 * @author xiaofanku
 * @since 20210413
 */
@FeignClient(value="TROLL-GATEWAY",
        contextId="letterClient",
        fallbackFactory= LetterClient.LetterFallbackFactory.class,
        configuration = FrontFeignClientConfig.class
)
@RequestMapping(value="/letter")
public interface LetterClient {
    //1.指定日期以来未坊的信件
    @GetMapping("/recent/{member}/{sender}/{stamp}")
    List<LetterDigestVo> getByPrevious(
            @PathVariable("member") long memberId,
            @PathVariable("sender") long sender,
            @PathVariable("stamp") int previousUx);

    //2.聊天界面中的记录
    @GetMapping("/chat/{member}/{sender}/{page}/{number}")
    EntityPageResult<LetterDigestVo> getMutual(
            @PathVariable("member") long memberId,
            @PathVariable("sender") long sender,
            @PathVariable("page") int page,
            @PathVariable("number") int pageSize);

    //3.会员的收件箱
    @GetMapping("/inbox/{member}/{page}/{number}")
    EntityPageResult<LetterInboxVo> getInbox(
            @PathVariable("member") long memberId,
            @PathVariable("page") int page,
            @PathVariable("number") int pageSize);

    //4.会员的发件箱
    @GetMapping("/outbox/{member}/{typed}/{page}/{number}")
    EntityPageResult<LetterOutboxVo> getOutbox(
            @PathVariable("member") long memberId,
            @PathVariable("typed") int typeEnumSymbol,
            @PathVariable("page") int page,
            @PathVariable("number") int pageSize);

    //5.发送消息
    @PostMapping(value="/sent", consumes="application/x-www-form-urlencoded")
    ResponseEntity<LetterDigestVo> send(
            @RequestParam("sender")long sender,
            @RequestParam("nickname")String senderNickname,
            @RequestParam("subject")String subject,
            @RequestParam("message")String content,
            @RequestParam("receiver")long receiver,
            @RequestParam("receiverNickname")String receiverNickname);

    //6.收件箱中标记信件为已读
    @PutMapping(value = "/inbox/{member}/readed/all", consumes="application/x-www-form-urlencoded")
    boolean readAll(
            @PathVariable("member")long memberId,
            @RequestParam("target")long othMember,
            @RequestParam("direction")int direction);

    //7.收件箱中标记信件为已读
    @PutMapping(value = "/inbox/{member}/readed", consumes = "application/x-www-form-urlencoded")
    boolean readed(
            @PathVariable("member")long memberId,
            @RequestParam("records")String idString);

    //8.收件箱中未读的信件数量
    @GetMapping("/inbox/{member}/unreaded/size")
    long getUnReadSize(@PathVariable("member")long memberId);

    //9.收件箱中指定发件人发来的未读的信件数量
    @GetMapping("/inbox/{member}/sender/{sender}/unreaded/size")
    long getUnReadSize(@PathVariable("member")long memberId, @PathVariable("sender")long senderId);

    //10.收件箱中发件人发来的未读数量
    @PostMapping(value="/inbox/{member}/unreaded", consumes="application/x-www-form-urlencoded")
    Map<Long,Long> getInboxSenderUnreads(
            @PathVariable("member")long memberId,
            @RequestParam("records")String senderIdString);

    //11.收件箱中发件人发来的未读数量
    @PostMapping(value = "/inbox/{member}/unreaded/collect", consumes="application/x-www-form-urlencoded")
    List<LetterInboxNoticeVo> getInboxSenderUnreadsCollect(
            @PathVariable("member")long memberId,
            @RequestParam("records")String senderIdString);

    //12.今日的通知
    @GetMapping("/today/claim")
    List<LetterDigestVo> getTodayClaim();

    @Component
    static class LetterFallbackFactory implements FallbackFactory<LetterClientFallback> {
        @Override
        public LetterClientFallback create(Throwable cause) {
            return new LetterClientFallback();
        }
    }

    static class LetterClientFallback implements LetterClient{

        @Override
        public List<LetterDigestVo> getByPrevious(long memberId, long sender, int previousUx) {
            return Collections.emptyList();
        }

        @Override
        public EntityPageResult<LetterDigestVo> getMutual(long memberId, long sender, int page, int pageSize) {
            return EntityPageResult.empty(1);
        }

        @Override
        public EntityPageResult<LetterInboxVo> getInbox(long memberId, int page, int pageSize) {
            return EntityPageResult.empty(1);
        }

        @Override
        public EntityPageResult<LetterOutboxVo> getOutbox(long memberId, int typeEnumSymbol, int page, int pageSize) {
            return EntityPageResult.empty(1);
        }

        @Override
        public ResponseEntity<LetterDigestVo> send(long sender, String senderNickname, String subject, String content, long receiver, String receiverNickname) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public boolean readAll(long memberId, long othMember, int direction) {
            return false;
        }

        @Override
        public boolean readed(long memberId, String idString) {
            return false;
        }

        @Override
        public long getUnReadSize(long memberId) {
            return 0L;
        }

        @Override
        public long getUnReadSize(long memberId, long senderId) {
            return 0L;
        }

        @Override
        public Map<Long, Long> getInboxSenderUnreads(long memberId, String senderIdString) {
            return Collections.emptyMap();
        }

        @Override
        public List<LetterInboxNoticeVo> getInboxSenderUnreadsCollect(long memberId, String senderIdString) {
            return Collections.emptyList();
        }

        @Override
        public List<LetterDigestVo> getTodayClaim() {
            return Collections.emptyList();
        }
    }
}
